package com.wjj.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wjj.common.core.utils.MapstructUtils;
import com.wjj.common.mybatis.core.page.PageQuery;
import com.wjj.common.mybatis.core.page.TableDataInfo;
import com.wjj.common.core.utils.StringUtils;
import com.wjj.common.core.utils.ip.AddressUtils;
import com.wjj.common.log.event.OperLogEvent;
import com.wjj.system.domain.SysOperLog;
import com.wjj.system.domain.bo.SysOperLogBo;
import com.wjj.system.domain.vo.SysOperLogVo;
import com.wjj.system.mapper.SysOperLogMapper;
import com.wjj.system.service.ISysOperLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 操作日志 服务层处理
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {

    /**
     * 操作日志记录
     *
     * @param operLogEvent 操作日志事件
     */
    @Async
    @EventListener
    public void recordOper(OperLogEvent operLogEvent) {
        SysOperLogBo operLog = MapstructUtils.convert(operLogEvent, SysOperLogBo.class);
        // 远程查询操作地点
        operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
        insertOperlog(operLog);
    }

    @Override
    public TableDataInfo<SysOperLogVo> selectPageOperLogList(SysOperLogBo operLog, PageQuery pageQuery) {
        Map<String, Object> params = operLog.getParams();
        QueryWrapper qw = QueryWrapper.create()
            .like(SysOperLog::getOperIp, operLog.getOperIp(), StringUtils.isNotBlank(operLog.getOperIp()))
            .like(SysOperLog::getTitle, operLog.getTitle(), StringUtils.isNotBlank(operLog.getTitle()))
            .eq(SysOperLog::getBusinessType, operLog.getBusinessType(), operLog.getBusinessType() != null && operLog.getBusinessType() > 0)
            .in(SysOperLog::getBusinessType, Collections.singletonList(operLog.getBusinessTypes()), ArrayUtil.isNotEmpty(operLog.getBusinessTypes()))
            .eq(SysOperLog::getStatus, operLog.getStatus(), operLog.getStatus() != null && operLog.getStatus() > 0)
            .like(SysOperLog::getOperName, operLog.getOperName(), StringUtils.isNotBlank(operLog.getOperName()))
            .between(SysOperLog::getOperTime, params.get("beginTime"), params.get("endTime"), params.get("beginTime") != null && params.get("endTime") != null);

        if (StringUtils.isBlank(pageQuery.getOrderByColumn())) {
            qw.orderBy(SysOperLog::getOperId, false);
        }
        Page<SysOperLog> page = mapper.paginate(pageQuery.build(), qw);
        Page<SysOperLogVo> result = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalRow());
        result.setRecords(MapstructUtils.convert(page.getRecords(), SysOperLogVo.class));
        return TableDataInfo.build(result);
    }

    /**
     * 新增操作日志
     *
     * @param bo 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLogBo bo) {
        SysOperLog operLog = MapstructUtils.convert(bo, SysOperLog.class);
        operLog.setOperTime(new Date());
        mapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLogVo> selectOperLogList(SysOperLogBo operLog) {
        Map<String, Object> params = operLog.getParams();
        List<SysOperLog> sysOperLogs = mapper.selectListByQuery(QueryWrapper.create()
            .like(SysOperLog::getOperIp, operLog.getOperIp(), StringUtils.isNotBlank(operLog.getOperIp()))
            .like(SysOperLog::getTitle, operLog.getTitle(), StringUtils.isNotBlank(operLog.getTitle()))
            .eq(SysOperLog::getBusinessType, operLog.getBusinessType(), operLog.getBusinessType() != null && operLog.getBusinessType() > 0)
            .in(SysOperLog::getBusinessType, Collections.singletonList(operLog.getBusinessTypes()), ArrayUtil.isNotEmpty(operLog.getBusinessTypes()))
            .eq(SysOperLog::getStatus, operLog.getStatus(), operLog.getStatus() != null && operLog.getStatus() > 0)
            .like(SysOperLog::getOperName, operLog.getOperName(), StringUtils.isNotBlank(operLog.getOperName()))
            .between(SysOperLog::getOperTime, params.get("beginTime"), params.get("endTime"), params.get("beginTime") != null && params.get("endTime") != null)
            .orderBy(SysOperLog::getOperId, false));
        return MapstructUtils.convert(sysOperLogs, SysOperLogVo.class);

    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return this.removeByIds(Arrays.asList(operIds)) ? 1 : 0;
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLogVo selectOperLogById(Long operId) {
        SysOperLog sysOperLog = mapper.selectOneById(operId);
        return MapstructUtils.convert(sysOperLog, SysOperLogVo.class);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        mapper.deleteByQuery(QueryWrapper.create());
    }
}
